package users

import (
	"context"
	"product/common/validator"

	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CheckMobileExistLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewCheckMobileExistLogic Check valid mobile
func NewCheckMobileExistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckMobileExistLogic {
	return &CheckMobileExistLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CheckMobileExistLogic) CheckMobileExist(req *types.CheckMobileExistReq) (resp *types.CheckMobileExistResp, err error) {
	// 手机号验证
	if !validator.IsChineseMobile(req.Mobile) {
		return &types.CheckMobileExistResp{
			Message: "手机号不合法",
			Ok:      false,
		}, nil
	}

	exists, err := l.svcCtx.UsersModel.CheckPhoneExists(l.ctx, req.Mobile)
	if err == nil && exists {
		return &types.CheckMobileExistResp{
			Message: "手机号已存在",
			Ok:      false,
		}, nil
	}

	return &types.CheckMobileExistResp{
		Message: "手机号可用",
		Ok:      true,
	}, nil
}
